package com.ijunfu.aspect;

import com.ijunfu.domain.Person;
import com.ijunfu.domain.Student;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/12/15 10:41
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
@Aspect
@Component
public class GetRequestAspect {

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        Object[] args = joinPoint.getArgs();

        if(Objects.nonNull(args) && args.length>0 && args[0] instanceof Person) {

            Field nameField = Person.class.getDeclaredField("name");
            nameField.setAccessible(Boolean.TRUE);
            nameField.set(args[0], "ijunfu");
        }

        return joinPoint.proceed();

    }
}
